﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data;
using System.IO;

namespace IBS
{
    public partial class IbsDataContext
    {
        public void SaveHQCSupplierCode(HQCSUPPLIER_CODE supplerCode)
        {
            using (var connection = new OracleConnection(this.connectionString))
            {
                connection.Open();
                using (var command = new OracleCommand())
                {
                    command.Connection = connection;
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "edex.USP_ADD_HQCSUPPLIERCODE";
                    command.Parameters.Clear();
                    command.Parameters.AddWithValue("p_SITECODE", supplerCode.SITECODE);
                    command.Parameters.AddWithValue("p_SUPPLIERSHORTCODE", supplerCode.HQC_SHORTCODE);
                    command.Parameters.AddWithValue("p_SUPPLIERLONGNAME", supplerCode.HQC_LONGCODE);
                    command.ExecuteNonQuery();
                }
            }
        }

        public void DeleteHQCSupplierCode(Int64 siteCode)
        {
            using (var connection = new OracleConnection(this.connectionString))
            {
                connection.Open();
                using (var command = new OracleCommand())
                {
                    command.Connection = connection;
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "edex.USP_DELETE_HQCSUPPLIERCODE";
                    command.Parameters.Clear();
                    command.Parameters.AddWithValue("p_SITECODE", siteCode);
                    command.ExecuteNonQuery();
                }
            }
        }

    }

    public class Parser
    {
        private string path { get; set; }

        public Parser(string path)
        {
            this.path = path;
        }

        public List<HQCSUPPLIER_CODE> parseFile()
        {
            string line;
            int counter = 1;
            List<HQCSUPPLIER_CODE> hqcSupplierList = new List<HQCSUPPLIER_CODE>();
            using (System.IO.StreamReader file = new StreamReader(this.path))
            {
                while ((line = file.ReadLine()) != null)
                {
                    if (counter > 5)//парсим с 6 строки
                    {
                        //код станции
                        //короткий код поставщика
                        //длинный код поставщика
                        if (line.Length == 109)
                        {
                            if (line.Substring(66, 20).Trim() != "<null>")
                            {
                                HQCSUPPLIER_CODE suppLine = new HQCSUPPLIER_CODE();
                                suppLine.SITECODE = Int64.Parse(line.Substring(0, 12).Trim());
                                suppLine.HQC_SHORTCODE = Int32.Parse(line.Substring(62, 4).Trim());
                                suppLine.HQC_LONGCODE = Int64.Parse(line.Substring(66, 20).Trim());
                                hqcSupplierList.Add(suppLine);
                            }
                        }
                    }
                    counter++;
                }
                file.Close();
            }
            return hqcSupplierList;
        }
    }

}
